#-*-coding:utf-8-*-
import os
import shutil

#该脚本测试对返回的节点信息进行复制操作

data={'ReadGeo1': ['ReadGeo2', 'E:/PycharmOut/NukeOut/For_Nuke/TEST/DuSheWithEye.abc'], 'Read1': ['Read', 'E:/PycharmOut/NukeOut/For_Nuke/TEST/QB2/QB_New_New.%04d.exr'], 'Read2': ['Read', 'E:/PycharmOut/NukeOut/For_Nuke/TEST/a002.JPEG'], 'Vectorfield1': ['Vectorfield', 'E:/PycharmOut/NukeOut/For_Nuke/TEST/aa.lut'], 'Camera1': ['Camera2', 'E:/PycharmOut/NukeOut/For_Nuke/TEST/xxss-13-10-017_mm_v001.fbx']}
OUT_PATH=r'E:\PycharmOut\NukeOut\For_Nuke\TEST\OUT'

def mz_copy(origin_paths,name_list,dir_path):
    """
    这里是打包序列帧
    :param origin_paths: 原文件绝对路径列表
    :param name_list:  原文件名称列表
    :param dir_path: 输出文件目录
    :return:
    """
    per_count=len(origin_paths)
    percent=0
    count=0
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    for i in origin_paths:
        des=os.path.join(dir_path,name_list[origin_paths.index(i)])
        shutil.copy(i,des)
        count+=1
    if per_count:
        percent=count*100/per_count
    return percent

def show_sequence(dir_path,base_name):
    """
    通过输入目录与文件通用名称,需要检测是否为文件，不能因为文件夹名称与文件通过名一样而导致失败
    :param dir_path:
    :param base_name: 文件通用名称
    :return:
    """
    sequence=[]
    file_names=[]
    file_list=os.listdir(dir_path)
    # print('file_list:',file_list)
    for i in file_list:
        abs_path=os.path.join(dir_path,i)
        if os.path.isfile(abs_path):
            file_names.append(i)
            if base_name in i:
                sequence.append(abs_path)
    return sequence,file_names

def check_file(path):
    """
    通过nuke读取的路径来获取其对应路径下的所有同名文件序列
    :param path:
    :return:
    """
    file_list = []
    name_list = []
    dir_path,file_name=os.path.split(path)
    if file_name.count('%0')>0:
        print('是序列')
        base_name=file_name.split('%0')[0] #序列文件通用名称
        file_list,name_list=show_sequence(dir_path,base_name)
    else:
        if os.path.isfile(path):
            file_list=[path]
            name_list=[os.path.split(path)[1]]
            print('不是序列')
    return file_list,name_list


for k,v in data.items():
    dir_path=os.path.join(OUT_PATH,v[0],k)
    # print(dir_path)
    file_paths, name_list = check_file(v[1])
    per_count=len(file_paths)  #该节点对应的总文件个数
    mz_copy(file_paths,name_list,dir_path)



# r1=data['Read2'][1]
# print('r1:',r1)
# file_paths,name_list=check_file(r1)
# print('file_paths:',file_paths)
# print('name_list:',name_list)
# mz_copy(file_paths,name_list,OUT_PATH)


# r1=data['ReadGeo1']
# if r1[0]=='ReadGeo2': #表示该路径geo路径
#     check_file(r1[1])
